<template>
	<view class="card">
		<view class="header">
			<view class="cardTitle">
				<text>{{title}}</text>
			</view>
			<view class="moreBtn" @click="toMore">
				<text>更多</text>
				<image src="../static/images/gengduo.png" mode="widthFix"></image>
			</view>
		</view>

		<scroll-view scroll-x="true" style="width: 750rpx;">
			<view class="cardContent" :style="{ width: 230 * list.length + 60 + 'rpx', paddingTop: showShadow ? '10rpx' : '0' }">
				<view class="item" v-for="(item,index) in list" :key="index" @click="toDetail(item.id)">
					<view class="imgBox">
						<image :src="item.picUrl" mode="widthFix"></image>
						<view class="playCount" v-if="item.playCount">
							<image src="/static/images/play-a.png" mode="widthFix"></image>
							<text>{{$tools.calcPlayCount(item.playCount)}}</text>
						</view>
						<view class="shadow" v-if="showShadow"></view>
					</view>
					<view class="itemTitle">
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			title: {
				type: String,
				default: ''
			},
			list: {
				type: Array,
				default: []
			},
			itemTitleKey: {
				type: String,
				default: 'name'
			},
			itemImgKey: {
				type: String,
				default: 'picUrl'
			},
			itemPlayCountKey: {
				type: String,
				default: 'playCount'
			},
			showShadow: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				// 可跳转的页面列表
				pageList: {
					'推荐歌单': '/pages/music/songList/songList',
					'推荐MV': '/pages/video/MV/MV',
					'推荐电台': '/pages/music/FM/FM'
				}
			}
		},
		watch: {
			list() {
				this.transitionKey()
			}
		},
		methods: {
			// 转换list内的键以使列表内的图片和标题键对应
			transitionKey() {
				if (this.itemTitleKey != 'name') {
					console.log('标题键转换')
					this.list.map(item => item.name = item[this.itemTitleKey])
				}
				if (this.itemImgKey != 'picUrl') {
					console.log('图片键转换')
					this.list.map(item => item.picUrl = item[this.itemImgKey])
					// this.list = this.list.filter(item => item.picUrl == '')
				}
				if (this.itemPlayCountKey != 'playCount') {
					this.list.map(item => item.playCount = item[this.itemPlayCountKey])
					console.log('播放量键转换')
				}
			},
			// 点击更多按钮时页面跳转
			toMore() {
				let pageUrl = this.pageList[this.title]
				uni.navigateTo({
					url: pageUrl,
					fail(err) {
						console.log(err)
					}
				})
			},
			// 点击某一项时跳转详情
			toDetail(id) {
				if (this.title == '推荐歌单') {
					uni.navigateTo({
						url: `/pages/music/songList/songListDetail?id=${id}`
					})
				} else if (this.title == '推荐MV') {
					uni.navigateTo({
						url: `/pages/video/MV/mvDetail?id=${id}`
					})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.card {
		width: 750rpx;
		border-radius: 20rpx;
		background-color: #fff;

		.header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 750rpx;
			height: 100rpx;
			padding: 0 30rpx;

			.cardTitle {
				font-size: 34rpx;
				letter-spacing: 3rpx;
				font-weight: 800;
			}

			.moreBtn {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 130rpx;
				height: 50rpx;
				border-radius: 25rpx;
				border: 1rpx solid #ddd;

				image {
					width: 20rpx;
				}
			}
		}

		.cardContent {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 30rpx 20rpx;

			// height: 200rpx;
			.item {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 210rpx;

				.imgBox {
					position: relative;
					width: 210rpx;
					border-radius: 20rpx;
					.playCount {
						position: absolute;
						display: flex;
						justify-content: center;
						align-items: center;
						right: 8rpx;
						top: 8rpx;
						height: 30rpx;
						border-radius: 15rpx;
						padding: 0 12rpx;
						color: #fff;
						font-size: 21rpx;
						background-color: rgba(0, 0, 0, .2);

						image {
							width: 20rpx;
							margin-right: 3rpx;
						}
					}

					.shadow {
						position: absolute;
						top: -10rpx;
						left: 20rpx;
						width: 170rpx;
						height: 10rpx;
						background-color: rgba(242, 242, 242, .9);
						border-top-left-radius: 10rpx;
						border-top-right-radius: 10rpx;
						// z-index: 5;
					}

					image {
						width: 210rpx;
						border-radius: 20rpx;
					}
				}

				.itemTitle {
					width: 210rpx;
					height: 70rpx;
					font-size: 24rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}
		}
	}
</style>
